package com.qdxwx.data.service.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.course.CourseAlbumMapper;
import com.qdxwx.models.course.CourseAlbum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程专集分类接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CourseAlbumServiceImpl implements CourseAlbumService {
    @Autowired
    private CourseAlbumMapper courseAlbumMapper;
    @Autowired
    private CourseAlbumItemService courseAlbumItemService;

    /**
     * 查询课程专集分类
     */
    @Override
    public CourseAlbum get(int id) {
        if (id < 1) return null;
        return courseAlbumMapper.selectCourseAlbumById(id);
    }

    /**
     * 修改课程专集分类
     */
    @Override
    public int update(CourseAlbum courseAlbum) {
        if (courseAlbum.getId() > 0) {
            return courseAlbumMapper.updateCourseAlbum(courseAlbum);
        }
        return 0;
    }

    /**
     * 添加课程专集分类
     */
    @Override
    public int insert(CourseAlbum courseAlbum) {
        courseAlbum.setCreateTime(LocalDateTime.now());
        return courseAlbumMapper.createCourseAlbum(courseAlbum);
    }

    /**
     * 删除课程专集分类及分类下课程专集
     */
    @Override
    public int delete(int id) {
        if (id > 0) {

            courseAlbumItemService.deleteByAlbumId(id);
           return courseAlbumMapper.deleteCourseAlbumById(id);
        }
        return 0;
    }

    @Override
    public List<CourseAlbum> getList() {
        return getList(new CourseAlbum(), null);
    }

    @Override
    public List<CourseAlbum> getList(CourseAlbum courseAlbum, PageRequest page) {
        return courseAlbumMapper.selectCourseAlbumList(courseAlbum, page);
    }

    @Override
    public int count(CourseAlbum courseAlbum) {
        return courseAlbumMapper.countCourseAlbum(courseAlbum);
    }
}